package com.jbookshelf.beans;


import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jbookshelf.dal.entities.Publishers;
import com.jbookshelf.dal.providers.PublishersDataProvider;

@Component
@Scope("request")
public class PublisherBean {

	private Publishers newPublisher;
	private Publishers selectedPublisher;
	private List<Publishers> publishers;

	@Autowired
	PublishersDataProvider dataProvider;

    public PublisherBean(){
        newPublisher = new Publishers();
        selectedPublisher = new Publishers();
    }

	public Publishers getNewPublisher() {
		return newPublisher;
	}

	public List<Publishers> getPublishers() {

		if (publishers == null) {
			publishers = dataProvider.getPublishers();
		}
		return publishers;
	}

	public Publishers getSelectedPublisher() {
		return selectedPublisher;
	}

	public void setNewPublisherName(String name) {
		this.newPublisher.setName(name);
	}

	public void setNewPublisherWebsite(String website) {
		this.newPublisher.setWebsite(website);
	}

	public String getNewPublisherName() {
		return newPublisher.getName();
	}

	public String getNewPublisherWebsite() {
		return newPublisher.getWebsite();
	}

	public void setSelectedPublisher(Publishers selectedPublisher) {
		this.selectedPublisher = selectedPublisher;
	}



	public void showEditDialog() {
		if (publishers.contains(selectedPublisher)) {
			final RequestContext context = RequestContext.getCurrentInstance();
			context.execute("editdlg.show()");
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Failed!",
					"No publisher selected!"));
		}
	}

	public void showViewDialog() {
		if (publishers.contains(selectedPublisher)) {
			final RequestContext context = RequestContext.getCurrentInstance();
			context.execute("viewdlg.show()");
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Failed!",
					"No publisher selected!"));
		}
	}


    public void showCreateDialog() {
        newPublisher = new Publishers();
        final RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createdlg.show()");

    }


    public void saveNewPublisher() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg;
        boolean savePublisher;

        if( newPublisher.getName() != null && dataProvider.validatePublisher(newPublisher)) {
            dataProvider.setPublisher(newPublisher);
            savePublisher = true;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Publisher added successfully", newPublisher.getName());
        } else {
            savePublisher = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Wrong publisher", "invalid name");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("savePublisher", savePublisher);
    }

}
